package com.zhs.gccs.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.DropDownAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/9-12:26
 * Email: renweiwei@ufashion.com
 */

public class DialogUtils {
    public static interface OnCancelListener {
        void onCanceled();
    }
    public static interface OnConfirmListener {
        void onConfirmed();
    }
    public static interface OnSingleChoiceListener2 {
        void onSingleChoice(Dialog dialog, int position);
    }
    public static interface DialogProgressListerer {
        void onProgress(int progress);
    }
    public static interface OnConfirmInputListener {
        void onConfirmed(String text);
    }

    public static interface OnConfirmDateListener {
        void onConfirmedDate(Dialog dialog, String value);
    }
    public static interface OnConfirmDateTimeListener {
        void onConfirmedDate(Date date);
    }

    public static void showEditablePromote2(Context mContext, String title,
                                            final OnCancelListener onCancelListener,
                                            final OnConfirmInputListener onConfirmListener) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = new Dialog(mContext);//builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_prompt_editable, null);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText(title);
        EditText et_message = (EditText) view.findViewById(R.id.et_message);
        et_message.requestFocus();
        et_message.setFocusableInTouchMode(true);
//        InputMethodManager inputMethodService = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//        inputMethodService.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onCancelListener != null) onCancelListener.onCanceled();
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String val = et_message.getText().toString();
                if(TextUtils.isEmpty(val))return;
                dialog.dismiss();
                if (onConfirmListener != null) onConfirmListener.onConfirmed(val);
            }
        });
        dialog.setContentView(view);
        dialog.show();
    }

    public static Dialog showProgressDialog(Context mContext, String message,DialogProgressListerer progressListerer) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_progress, null);
        ProgressBar id_progress =  view.findViewById(R.id.progressBar);
        TextView tv_promote =  view.findViewById(R.id.tv_promote);
        tv_promote.setText(message);
        dialog.show();
        dialog.setContentView(view);
        return  dialog;
    }

    public static void showPromote1(Context mContext, String message, final OnConfirmListener onConfirmListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_prompt1, null);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onConfirmListener != null) onConfirmListener.onConfirmed();
            }
        });
        tv_message.setText(message);
        dialog.show();
        dialog.setContentView(view);
    }

    public static void showPromote2(Context mContext, String message, final OnCancelListener onCancelListener, final OnConfirmListener onConfirmListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_prompt2, null);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onCancelListener != null) onCancelListener.onCanceled();
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onConfirmListener != null) onConfirmListener.onConfirmed();
            }
        });
        tv_message.setText(message);
        dialog.show();
        dialog.setContentView(view);
    }

    public static void showOpenAppSettingDialog(Context mContext) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final Dialog dialog = builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_prompt2, null);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                IntentUitls.launchAppDetailsSettings();
            }
        });
        tv_message.setText(mContext.getString(R.string.permission_denied_forever_message));
        dialog.show();
        dialog.setContentView(view);
    }

    public static void showSingleChoice2(Context mContext, String title, List data, int selection,
                                         final OnSingleChoiceListener2 onSingleChoiceListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final AlertDialog dialog = builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_single_choice, null);
        ListView listView = (ListView) view.findViewById(R.id.listView);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        final DropDownAdapter adapter = new DropDownAdapter(mContext, data);
        listView.setAdapter(adapter);
        adapter.setCheckItem(selection);
        tv_title.setText(title);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setCheckItem(position);
                if (onSingleChoiceListener != null)
                    onSingleChoiceListener.onSingleChoice(dialog, position);
            }
        });
        dialog.setView(view);
        dialog.show();
    }

    public static void showDateDialog(Context mContext, final OnCancelListener onCancelListener,
                                      final OnConfirmDateListener onConfirmDateListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final AlertDialog dialog = builder.create();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dailog_date_time, null);
        final MaterialCalendarView calendarView = (MaterialCalendarView) view.findViewById(R.id.calendarView);
        calendarView.setDateSelected(new Date(), true);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (onCancelListener != null) onCancelListener.onCanceled();
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                dialog.dismiss();
                CalendarDay calendarDay = calendarView.getSelectedDate();
                if (calendarDay == null) return;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String txt = simpleDateFormat.format(calendarDay.getDate());//calendarDay.getYear()+"-"+(calendarDay.getMonth()+1)+"-"+calendarDay.getDay();
                if (TextUtils.isEmpty(txt)) return;
                if (onConfirmDateListener != null)
                    onConfirmDateListener.onConfirmedDate(dialog, txt);
            }
        });
//        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                return true;
//            }
//        });
        dialog.setView(view);
        dialog.show();
    }

//    public static void showDateTimeDialog(Context mContext,String defaultDate, final OnConfirmDateTimeListener onConfirmDateTimeListener) {
//        TimePickerBuilder builder =  new TimePickerBuilder(mContext, new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                if(onConfirmDateTimeListener!=null){
//                    onConfirmDateTimeListener.onConfirmedDate(date);
//                }
//            }
//        })
//                .setType(new boolean[]{true, true, true, true, true, false})// 默认显示： 年月日
////                        .setCancelText("取消")//取消按钮文字
////                        .setSubmitText("确定")//确认按钮文字
////                        .setContentSize(18)//滚轮文字大小
////                        .setTitleSize(20)//标题文字大小
//                .setTitleText("选择时间")//标题文字
//                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
//                .isCyclic(true)//是否循环滚动
////                        .setTitleColor(Color.BLACK)//标题文字颜色
////                        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
////                        .setCancelColor(Color.BLUE)//取消按钮文字颜色
////                        .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
////                        .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
////                        .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
////                        .setRangDate(startDate, endDate)//起始终止年月日设定
//                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
////                        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .isDialog(true);//是否显示为对话框样式
//        if(!TextUtils.isEmpty(defaultDate)){
//            Calendar date = Calendar.getInstance();
//            date.setTime(DateUtils.parseDate(defaultDate));
//            builder.setDate(date);
//        }
//        TimePickerView pvTime = builder.build();
//        pvTime.show();
//    }

}
